package com.atguigu.eduservice.service.impl;

import com.atguigu.eduservice.client.VodClient;
import com.atguigu.eduservice.entity.EduCourse;
import com.atguigu.eduservice.entity.EduCourseDescription;
import com.atguigu.eduservice.entity.EduVideo;
import com.atguigu.eduservice.entity.vo.CourseInfoVo;
import com.atguigu.eduservice.entity.vo.CoursePublishVo;
import com.atguigu.eduservice.entity.vo.CourseQuery;
import com.atguigu.eduservice.mapper.EduCourseMapper;
import com.atguigu.eduservice.service.EduChapterService;
import com.atguigu.eduservice.service.EduCourseDescriptionService;
import com.atguigu.eduservice.service.EduCourseService;
import com.atguigu.eduservice.service.EduVideoService;
import com.atguigu.servicebase.config.exceptionhandler.GuliException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author wangshuai
 * @since 2021-02-04
 */
@Service
public class EduCourseServiceImpl extends ServiceImpl<EduCourseMapper, EduCourse> implements EduCourseService {

    @Autowired
    private EduCourseDescriptionService eduCourseDescriptionService;

    @Autowired
    private EduChapterService chapterService;

    @Autowired
    private EduVideoService videoService;

    @Override
    public String addCourseInfo(CourseInfoVo courseInfoVo) {

        //1.向课程表李添加课程基本信息
        //CourseInfoVo转换为eduCourse
        EduCourse eduCourse = new EduCourse();

        BeanUtils.copyProperties(courseInfoVo, eduCourse);

        int insert = baseMapper.insert(eduCourse);

        if(insert <= 0) {
            throw new GuliException(20001, "添加课程信息失败");
        }

        //获取到课程id赋值给课程描述的id
        String cid = eduCourse.getId();

        //2.向简介中添加
        EduCourseDescription eduCourseDescription = new EduCourseDescription();

        BeanUtils.copyProperties(courseInfoVo, eduCourseDescription);

        //保持和eduCourse中的id一样
        eduCourseDescription.setId(cid);

        eduCourseDescriptionService.save(eduCourseDescription);

        return cid;

    }

    @Override
    public CourseInfoVo showDetail(String courseId) {

        //1.查询课程表
        EduCourse course = baseMapper.selectById(courseId);
        CourseInfoVo courseInfoVo = new CourseInfoVo();
        BeanUtils.copyProperties(course,courseInfoVo);

        //2.查询描述表
        EduCourseDescription description = eduCourseDescriptionService.getById(courseId);
        courseInfoVo.setDescription(description.getDescription());

        return courseInfoVo;
    }

    @Override
    public String updateCourseInfo(CourseInfoVo courseInfoVo) {
        //修改课程表
        EduCourse eduCourse = new EduCourse();

        BeanUtils.copyProperties(courseInfoVo, eduCourse);

        int update = baseMapper.updateById(eduCourse);

        if(update <= 0) {
            throw new GuliException(20001, "修改课程信息失败");
        }

        String cid = eduCourse.getId();

        //修改描述
        EduCourseDescription description = new EduCourseDescription();
        description.setId(eduCourse.getId());
        description.setDescription(courseInfoVo.getDescription());
        eduCourseDescriptionService.updateById(description);

        return cid;
    }

    @Override
    public CoursePublishVo publishCourseInfo(String id) {

        CoursePublishVo publishCourseInfo = baseMapper.getPublishCourseInfo(id);

        return publishCourseInfo;
    }

    @Override
    public Map<String,Object> getAllCoursePageList(Long page, Long limit, CourseQuery courseQuery) {

        String title = courseQuery.getTitle();

        String teacherId = courseQuery.getTeacherId();

        String subjectParentId = courseQuery.getSubjectParentId();

        String subjectId = courseQuery.getSubjectId();

        QueryWrapper<EduCourse> wrapperCourse = new QueryWrapper<>();

        if (!StringUtils.isEmpty(title)) {
            wrapperCourse.like("title",title);
        }

        if (!StringUtils.isEmpty(teacherId)) {
            wrapperCourse.eq("teacher_id",teacherId);
        }

        if (!StringUtils.isEmpty(subjectParentId)) {
            wrapperCourse.eq("subject_parent_id",subjectParentId);
        }

        if (!StringUtils.isEmpty(subjectId)) {
            wrapperCourse.eq("subject_id",subjectId);
        }

        Page<EduCourse> coursePage = new Page<>(page,limit);

        IPage<EduCourse> courseIPage = baseMapper.selectPage(coursePage,wrapperCourse);

        Long total = courseIPage.getTotal();

        List<EduCourse> records = courseIPage.getRecords();

        Map map = new HashMap();

        map.put("total",total);

        map.put("records",records);

        return map;
    }

    @Override
    public Boolean deleteCourse(String id) {

        videoService.removeByCourseId(id);

        chapterService.removeByCourseId(id);

        eduCourseDescriptionService.removeByCourseId(id);

        Integer result = baseMapper.deleteById(id);

        return result != null && result > 0;
    }

    @Override
    public List<Map> getAllCourse() {

        List<Map> list = baseMapper.getAllCourse();

        return list;
    }
}
